﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Input;

namespace Saral.Command
{
    public class SaralCommand : ICommand
    {

        readonly Action<object> executeLogic;

        readonly Predicate<object> canExecuteCondition;

        readonly bool predicateFlag;

        public SaralCommand(Action<object> executeLogic, Predicate<object> canExecuteCondition)
        {
            this.executeLogic = executeLogic;
            this.canExecuteCondition = canExecuteCondition;
            this.predicateFlag = false;
        }

        public SaralCommand(Action<object> executeLogic)
        {
            this.executeLogic = executeLogic;
            this.canExecuteCondition = null;
            this.predicateFlag = true;
        }

        public bool CanExecute(object parameter)
        {
            if (predicateFlag == false)
            {
                return canExecuteCondition(parameter);
            }

            return true;
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public void Execute(object parameter)
        {
            executeLogic(parameter);
        }
    }
}
